uniform float volumeSpacing;
uniform sampler3D volume;
uniform sampler1D transferFunc;

//vec4 dataInterpolation( sampler3D data, const vec4 texCoords );
//vec3 gradient( sampler3D data, const float scaleValue, const vec4 texCoord, const float voxelSpacing );

void main() 
{        
   vec4 attributes = texture3D(volume, gl_TexCoord[0].xyz);
   //vec4 attributes = dataInterpolation(volume, gl_TexCoord[0]);
     
   vec3 grad = vec3(1.0, 0.0, 0.0);
   
   vec3 dx = vec3(volumeSpacing, 0, 0);
   vec3 dy = vec3(0, volumeSpacing, 0);
   vec3 dz = vec3(0, 0, volumeSpacing);
   
   vec3 newX = vec3(gl_TexCoord[0].xyz);
   newX.x = newX.x + dx.x;
   vec3 newY = vec3(gl_TexCoord[0].xyz);
   newY.y = newY.y + dy.y;
   vec3 newZ = vec3(gl_TexCoord[0].xyz);
   newZ.z = newZ.z + dz.z;
   
   vec4 forwardDataX = texture3D(volume, newX);
   vec4 forwardDataY = texture3D(volume, newY);
   vec4 forwardDataZ = texture3D(volume, newZ);
   
   grad.x = forwardDataX.a - attributes.a;
   grad.y = forwardDataY.a - attributes.a;
   grad.z = forwardDataZ.a - attributes.a;
   
   //substitute gradient calculation here:
   //gradient = gradient(volume, attributes.a, gl_TexCoord[0], volumeSpacing);

   vec3 normal = grad * (-1.0);
   normal = normalize(normal);
   
   vec4 processedAttributes = texture1D( transferFunc, attributes.a );
      
   vec3 lightDir = vec3(1.0, 0.0, 0.0);
   lightDir = normalize(lightDir);
   
   vec4 diffuse = vec4(0.4, 0.4, 0.4, 1.0);
   vec4 specular = vec4(0.7, 0.7, 0.7, 1.0);
     
   float dotProduct = dot(normal, lightDir);
   if(dotProduct > 0.0) dotProduct = 0.0;
   vec4 lightRadiance = diffuse * dotProduct * (-1.0);
   
   //calculate specular term
   //specify V - viewing vector (eye vector)
   //and alpha - specular power argument
   
   vec3 V = vec3(0.0, 1.0, 1.0);
   V = normalize(V);
   //vec3 L = vec3(0.0, 1.0, 0.0);
   vec3 L = lightDir * (-1.0);
   vec3 H = L + V;
   H = normalize(H);
   float alpha = 8.0;
   float specularDot = dot(normal, H);
   if(specularDot < 0.0) specularDot = 0.0;
   //specularDot = specularDot * (-1.0);
   float specularComponent = pow(specularDot, alpha);
   //comment the following line out if you don't want to add specular
   lightRadiance = lightRadiance + specular*specularComponent;
   
   vec4 intensity = lightRadiance + processedAttributes;
   intensity.a = processedAttributes.a;

   gl_FragColor = intensity;
}
